go to previous page   go to home page   go to next page

Answer:

drawOval( 100-50, 300-50, 2*50, 2*50 )

Of course the method would work if you did the arithmetic and put the results in the method call.


Drawing Rectangles

import java.swing.JApplet;
import java.awt.*;

// assume that the drawing area is 150 by 150
public class SquareAndRectangle extends JApplet
{
  final int areaSide = 150 ;
  final int width = 100, height = 50;

  public void paint ( Graphics gr )
  { 
    gr.setColor( Color.white );
    gr.fillRect( 0, 0, 150, 150 );
    gr.setColor( Color.blue );

    // outline the drawing area
    gr.drawRect( 0, 0, areaSide-1, areaSide-1 ); 

    // draw interior rectangle.
    gr.drawRect( areaSide/2 - width/2 , 
        areaSide/2 - height/2, width, height ); 
   }
}

To draw a rectangle, use the drawRect() method of a Graphics object. This method looks like:

drawRect(int  x, int  y, 
    int  width, int  height)

It draws the outline of a rectangle using the current pen color. The left and right edges of the rectangle are at x and x + width respectively. The top and bottom edges of the rectangle are at y and y + height respectively.

This method is also used to draw a square. This applet draws a rectangle around the entire drawing area, then puts another rectangle in the center.

Here is what it draws on your screen:

Not all browsers can run applets. If you see this, yours can not.

In drawing the outer square, the value 1 was subtracted from its width and height so that the edges would be inside the drawing area.


QUESTION 7:

What do you suppose the following method does?

gr.setColor( Color.blue );